package tp.pr4.items;

import tp.pr4.NavigationModule;
import tp.pr4.Place;
import tp.pr4.RobotEngine;
import tp.pr4.Street;

/**
 * An item that represents a code card. This item contains a code that can be
 * employed to open and close streets. It always remains in the robot inventory
 * after using it.
 */
public class CodeCard extends Item {

	private String code;

	/**
	 * Code card constructor
	 * 
	 * @param id
	 *            - Code Card name
	 * @param description
	 *            - Code Card description
	 * @param code
	 *            - Secret code stored in the code card
	 */
	public CodeCard(java.lang.String id, java.lang.String description,
			java.lang.String code) {
		super(id, description);
		this.code = code;
	}

	@Override
	/**
	 * A code card always can be used. Since the robot has the code card it never loses it
	 */
	public boolean canBeUsed() {
		return true;
	}

	@Override
	/**
	 * The method to use a code card. If the robot is in a place which contains a street in 
	 * the direction he is looking at, then the street is opened or closed if the street 
	 * code and the card code match
	 * @return - true If the codecard can complete the action of opening or closing a street. Otherwise
	 * it returns false.
	 */
	public boolean use(RobotEngine r, NavigationModule n) {
		Street s = n.getHeadingStreet();
		if (s == null)
			return false;
		else if (s.isOpen())
			return s.close(this);
		else
			return s.open(this);
	}

	/**
	 * Gets the code stored in the code card
	 * 
	 * @return A String that represents the code
	 */
	public java.lang.String getCode() {
		return code;
	}

}
